import { Player, world } from "@minecraft/server";
import { Game, GameComponent } from "@sapi-game/main";
import { MonopolyBackState } from "@/monopoly/states/backState";

export class MonopolyItems extends GameComponent<MonopolyBackState> {
    override onAttach(): void {
        this.bind("dfw:buy", (p) => {
            this.dfwclear(p);
            p.runCommand(
                "execute as @s[scores={dfw_now=1}] run function dfw/act/buy"
            );
        });
        this.bind("dfw:skip", (p) => {
            this.dfwclear(p);
            p.runCommand(
                "execute as @s[scores={dfw_now=1}] run function dfw/next"
            );
        });
        this.bind("dfw:up", (p) => {
            this.dfwclear(p);
            p.runCommand(
                "execute as @s[scores={dfw_now=1}] run function dfw/act/up"
            );
        });
        this.bind("dfw:freeup", (p) => {
            p.runCommand(
                "execute as @s[scores={dfw_now=1}] unless entity @e[x=~,y=~-3,z=~,r=3,type=armor_stand,scores={dfw_id=1..}] run title @s subtitle 请站在房产上使用"
            );
            p.runCommand(
                "execute as @s[scores={dfw_now=1}] unless entity @e[x=~,y=~-3,z=~,r=3,type=armor_stand,scores={dfw_id=1..}] run title @s title §c升级失败"
            );
            p.runCommand(
                "execute as @s[scores={dfw_now=1}] run execute as @e[x=~,y=~-3,z=~,r=3,type=armor_stand,scores={dfw_id=1..}] at @s run function dfw/act/freeup"
            );
        });
        this.bind("dfw:plane", (p) => {
            p.runCommand(
                "execute as @s[scores={dfw_now=1}] unless entity @e[x=~,y=~-3,z=~,r=3,type=armor_stand,scores={dfw_id=1..}] run title @s subtitle 请站在目的地上使用"
            );
            p.runCommand(
                "execute as @s[scores={dfw_now=1}] unless entity @e[x=~,y=~-3,z=~,r=3,type=armor_stand,scores={dfw_id=1..}] run title @s title §c起飞失败"
            );
            p.runCommand(
                "execute as @s[scores={dfw_now=1}] run execute as @e[x=~,y=~-3,z=~,r=3,type=armor_stand,scores={dfw_id=1..}] at @s run function dfw/act/fly"
            );
        });
        this.bind("dfw:sell", (p) => {
            p.runCommand(
                "execute as @s unless entity @e[x=~,y=~-3,z=~,r=3,type=armor_stand,scores={dfw_id=1..}] run title @s subtitle 请站在要卖的房子上使用"
            );
            p.runCommand(
                "execute as @s unless entity @e[x=~,y=~-3,z=~,r=3,type=armor_stand,scores={dfw_id=1..}] run title @s title §c卖出失败"
            );
            p.runCommand(
                "execute as @s if entity @e[x=~,y=~-3,z=~,r=3,type=armor_stand,scores={dfw_id=1..}] at @s run function dfw/act/sell"
            );
        });
    }

    dfwclear(player: Player) {
        //如果不是大富翁参与者则不执行
        if (
            world.scoreboard.getObjective("dfw_team")?.getScore(player) ??
            0 <= 1
        )
            return;
        try {
            player.runCommand("function dfw/clearitem");
        } catch (e) {}
    }

    bind(typeId: string, func: (p: Player) => void) {
        this.subscribe(
            Game.events.itemUse,
            (t) => {
                func(t.source);
            },
            {
                itemId: typeId,
            }
        );
    }
}
